package com.hzit.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzit.page.dao.GoodsDao;
import com.hzit.page.dao.ItemDao;
import com.hzit.page.entity.TbGoods;
import com.hzit.page.entity.TbGoodsDesc;
import com.hzit.page.entity.TbItem;
import com.hzit.page.service.GoodsDescService;
import com.hzit.page.service.GoodsService;
import com.hzit.page.service.ItemCatService;
import com.hzit.page.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.context.Context;
import org.thymeleaf.context.IContext;
import org.thymeleaf.spring5.SpringTemplateEngine;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemDao, TbItem> implements ItemService {
	@Autowired
	private SpringTemplateEngine engine;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private GoodsDescService goodsDescService;
	@Autowired
	private ItemCatService itemCatService;


	//1. 根据商品id生成静态页面
	@Override
	public void createHtml(String id) throws IOException {
		//1.1 定义thymeleaf工作上下文环境对象
		Context context = new Context();
		//1.2 定义存放数据的集合
		Map<String, Object> mapData = new HashMap<>();
		//1.2.1 查询spu商品信息
		TbGoods goods = goodsService.getById(id);
		mapData.put("goods",goods);
		//1.2.2 查询出goods_desc商品描述信息
		TbGoodsDesc goodsDesc = goodsDescService.getById(id);
		//1.2.3 处理图片信息
		String itemImages = goodsDesc.getItemImages();
		List<Map> itemImagesList = JSON.parseArray(itemImages, Map.class);
		//1.2.4 处理自定义扩展属性
		List<Map> customAttributeItemsList = JSON.parseArray(goodsDesc.getCustomAttributeItems(), Map.class);
		//1.2.5 处理规格属性
		List<Map> specificationItemsList = JSON.parseArray(goodsDesc.getSpecificationItems(), Map.class);
		//1.2.6 将上面的属性都放到map中
		mapData.put("goodsDesc",goodsDesc);
		mapData.put("itemImagesList",itemImagesList);
		mapData.put("customAttributeItemsList",customAttributeItemsList);
		mapData.put("specificationItemsList",specificationItemsList);

		//1.3 查询出一、二、三级分类
		//1.3.1 查询出各个分类的名字
		String category1Name = itemCatService.getById(goods.getCategory1Id()).getName();
		String category2Name = itemCatService.getById(goods.getCategory2Id()).getName();
		String category3Name = itemCatService.getById(goods.getCategory3Id()).getName();
		//1.3.2 将分类的名字放到map中
		mapData.put("category1Name",category1Name);
		mapData.put("category2Name",category2Name);
		mapData.put("category3Name",category3Name);

		//1.4 查询sku商品信息
		//1.4.1 根据spu商品id，查询出sku商品列表并对is_default列进行倒序排序
		List<TbItem> items = this.list(new QueryWrapper<TbItem>().eq("goods_id", id).orderByDesc("is_default"));
		//1.4.2 将上面的sku商品列表放到map中
		mapData.put("itemList",items);
		mapData.put("item",items.get(0));



		//1.4 将数据集合与context进行绑定
		context.setVariables(mapData);
		//1.5 定义输出流对象(关联输出的文件)
		Writer writer = new FileWriter(new File("D:\\nginx-1.22.0\\html\\" + id + ".html"));
		//1.6 执行模板引擎
		engine.process("item",context,writer);
	}
}
